package com.hctforgreen.greenservice.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import cn.dns.reader.network.Util;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.ctr.DbController;
import com.hctforgreen.greenservice.model.BookListEntity.BookEntity;
import com.hctforgreen.greenservice.model.VideoTypeEntity.VideoDetailEntity.VideoPartEntity;
import com.umeng.analytics.MobclickAgent;

public class DownloadUtil {
	/**
	 * 确认下载对话框 针对资料
	 * 
	 * @param activity
	 * @param entity
	 * @param type 0未下载   1下载文件不存在
	 */
	public void makeSureDownloadDiaolg(final Activity activity, final BookEntity entity, final int type) {
		String sContent = "";
		if (type == 0) {
			sContent = activity.getString(R.string.make_sure_download_content_hint);
		} else {
			sContent = activity.getString(R.string.make_sure_redownload_content_hint);
		}
		sContent = String.format(sContent, entity.bookFileSize);

		Dialog dialog = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(entity.displayName)
				.setMessage(sContent)
				.setNegativeButton(R.string.dialog_cancel_hint, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						})
				.setPositiveButton(R.string.dialog_confirm_hint, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								doDownloadBook();
								dialog.dismiss();
							}
							private void doDownloadBook() {
								MobclickAgent.onEvent(activity, "Account_download");
								final DbController dbController = new DbController(activity);
								entity.doWhatClick = HctConstants.ON_ACTIVITY_KEY_DO_DOWNLOAD_CLICK;
								dbController.updateBookOnWhatClick(entity);
								/**
								 * 调用Service下载
								 */
								Util.doDownloadBook(activity, entity);
							}
						});
		dialog = builder.create();
		dialog.show();
	}

	/**
	 * 确认下载对话框 针对视频
	 * 
	 * @param activity
	 * @param fileDirectoryPath
	 * @param type
	 *            0未下载 1下载文件不存在
	 */
	public void makeSureDownloadDiaolg(final Activity activity, final VideoPartEntity entity) {
		String sContent = activity
				.getString(R.string.make_sure_redownload_content_hint);
		sContent = String.format(sContent, entity.fileSize);
		Dialog dialog = null;
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(entity.videoName + "_" + entity.part)
				.setMessage(sContent)
				.setNegativeButton(R.string.dialog_cancel_hint, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						})
				.setPositiveButton(R.string.dialog_confirm_hint, new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								doDownloadVideo();
								dialog.dismiss();
							}
							private void doDownloadVideo() {
								Util.doDownloadVideo(activity, entity);
							}
						});
		dialog = builder.create();
		dialog.show();
	}
}
